var app = getApp();
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
  	longitude: '',
		latitude: '',
		froms:{},
		adds:''
  },
  onLoad(options) {
  	var mps=wx.getStorageSync('map')
  	this.setData({
      longitude:mps.longitude,
      latitude:mps.latitude,
      froms:wx.getStorageSync('userInfo')
 		});
 		
  	 this.mapCtx = wx.createMapContext('map');
  	 // 实例化API核心类
    qqmapsdk = new QQMapWX({
        key: 'UQ6BZ-UH3WI-5UZG7-5EZVY-LRGUV-REBTI'
    });
  },
  regionchange(e) {
  	var that=this;
    console.log(e)
    if(e.type=="end"){
    	this.mapCtx.getCenterLocation({
			success: function (r) {
				console.log(r);
				//地址你解析
				qqmapsdk.reverseGeocoder({
		      location: {
		        latitude: r.latitude,
		        longitude: r.longitude
		      },
		      success: function (res) {
		        console.log(res);
		        var str1 = 'froms.longitude';
		        var str2 = 'froms.latitude';
		        that.setData({
		        	[str1]:r.longitude,
      				[str2]:r.latitude,
				      adds:res.result.address_reference.landmark_l2.title +"("+ res.result.address +")"
				 		});
		      },
		      fail: function (res) {
		        console.log(res);
		      },
		      complete: function (res) {
		        // console.log(res);
		      }
		    });
				//
			}
		})
    
    }
    
  },
  formSubmit:function(e){
    console.log(e.detail.value)
  	var that=this;
  	var post=e.detail.value;
  	
  	if(post.address==""){
  		wx.showToast({
        title: "请填写详细地址",
        icon: "none"});
      	return false;
  	}
  	
  	app.globalAjax(
	  		'/api/store/apply',
	  		"post",
	  		post,
	  		function(e){
	  			if(e.code==0)
	      	{
	      		wx.showToast({
						    title: '提交成功',
						    icon: 'none',
						    duration: 2000,
						    success: function () {
						    	setTimeout(function(){
						    		wx.navigateBack({delta: 1})
						    	},2000)
						    }
						});
	      	}else{
	      		wx.showToast({
				        title: e.msg,
				        icon: "none"
				    });
	      	}
	  	})
  }
})